package com.craftplay.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.craftplay.common.ServerResponse;
import com.craftplay.model.*;
import com.craftplay.service.*;
import com.craftplay.util.AliMsgSendUtil;
import com.craftplay.util.ToolUntils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/play")
public class EnrollController {

    @Autowired
    HostHolder hostHolder;

    @Autowired
    EnrollService enrollService;

    @Autowired
    OfferService offerService;

    @Autowired
    UserService userService;

    @Autowired
    ScopeInfoService scopeInfoService;

    @Autowired
    MomentService momentService;

    @Autowired
    BannerImageService bannerImageService;

    @RequestMapping(method = RequestMethod.POST, value = "/enroll")
    @ResponseBody
    public ServerResponse<String> enroll(@RequestBody Enroll enroll) {
        if (enroll != null) {
            int unitId = enroll.getUnitId();
            int actorId = enroll.getActorId();
            enroll.setStatus(0);
            enroll.setCreatedTime(new Date());

            if (enrollService.selectByActorIdAndUnitId(actorId, unitId).size() == 0) {
                Unit unit = momentService.oneMomentDetail(unitId);
                int kind = unit.getKind();
                //如果是前三种情况, 要考虑报名人数限制
                if (kind == 1 || kind == 2 || kind == 3) {
                    //先得到已经通过报名的人数
                    int passedNum = enrollService.getPassedEnrollNumByUnitId(unitId);
                    //joinNum表示owner外可以报名的数量
                    int joinNum = 0;
                    if (kind == 1 || kind == 2) {
                        joinNum = unit.getJoinNum() - 1;
                    } else if (kind == 3) {
                        joinNum = unit.getJoinNum();
                    } else {

                    }
                    if (passedNum >= joinNum) {
                        return ServerResponse.createByErrorMessage("报名人数已达上限！");
                    }

                }
                if (enrollService.addEnroll(enroll) > 0) {
                    return ServerResponse.createBySuccessMessage("报名成功");
                } else {
                    return ServerResponse.createByErrorMessage("报名失败！");
                }

            } else {
                return ServerResponse.createByErrorMessage("已经报名请勿重复提交！");
            }
        }
        return ServerResponse.createByErrorMessage("报名失败！");
    }

    //根据某活动id列出该活动所有尚未处理的报名列表
    @RequestMapping(method = RequestMethod.GET, value = "/allenroll")
    @ResponseBody
    public ServerResponse<List<EnrollInfo>> listAllEnroll(@RequestParam("id") int id) {
        List<Enroll> enrollList = enrollService.listAllUnit(id);
        List<EnrollInfo> set = new ArrayList<>();
        for (Enroll enroll : enrollList) {
            User user = userService.selectUserById(enroll.getActorId());
            EnrollInfo enrollInfo = new EnrollInfo();
            enrollInfo.setEnrollId(enroll.getId());
            if (user != null) {
                enrollInfo.setUserId(user.getUserId());
                enrollInfo.setImgUrl(user.getHeadUrl());
                enrollInfo.setUserName(user.getNickname());
                enrollInfo.setSex(user.getSex());
            }
            enrollInfo.setDescrip(enroll.getDescrip());
            enrollInfo.setStatus(enroll.getStatus());
            enrollInfo.setCreatedTime(enroll.getCreatedTime());
            set.add(enrollInfo);
        }
        if (set != null) {
            return ServerResponse.createBySuccess(set);
        } else {
            return ServerResponse.createByErrorMessage("添加失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/enrolldelete")
    @ResponseBody
    public ServerResponse<String> deleteEnroll(int[] id) {
        boolean isSuccess = false;
        for (int item : id) {
            if (enrollService.updateEnroll(item, -1) > 0) {
                isSuccess = true;
            } else {
                isSuccess = false;
            }
        }

        if (isSuccess == true) {
            return ServerResponse.createBySuccessMessage("删除成功");
        } else {
            return ServerResponse.createByErrorMessage("删除失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/enrollpass")
    @ResponseBody
    public ServerResponse<String> addEnroll(int[] id) {
        boolean isSuccess = false;
        for (int item : id) {
            if (enrollService.updateEnroll(item, 1) > 0) {
                isSuccess = true;
                //通过报名報名发送短信提醒
                Enroll enroll = enrollService.getEnrollActorId(item);//获取报名者用户id
                User user = userService.selectUserById(enroll.getActorId());
                Unit unit = momentService.oneMomentDetail(enroll.getUnitId());
                if (StringUtils.isNotEmpty(user.getTel()) && unit != null) {
                    JSONObject json = new JSONObject().fluentPut("unitTitle", unit.getTitle());
                    try {
                        AliMsgSendUtil.sendSms(user.getTel(), "SMS_144853233", json);
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                isSuccess = false;
            }
        }

        if (isSuccess == true) {
            return ServerResponse.createBySuccessMessage("添加成功");
        } else {
            return ServerResponse.createByErrorMessage("添加失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/data")
    @ResponseBody
    public ServerResponse<Statistic> data(Integer id) {
        List<Enroll> enrollList = new ArrayList<Enroll>();
        List<Offer> offerList = new ArrayList<Offer>();

        enrollList = enrollService.getStatistic(id);
        offerList = offerService.getStatistic(id);

        if (enrollList != null && offerList != null) {
            Statistic stat = new Statistic();
            stat.setEnrollNum(enrollList.size());
            stat.setOfferNum(offerList.size());
            return ServerResponse.createBySuccess(stat);
        } else {
            return ServerResponse.createByErrorMessage("获取数据失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/scopeinfo")
    @ResponseBody
    public ServerResponse<List<ScopeInfo>> listAllScopeInfo() {
        List<ScopeInfo> scopeInfoList = new ArrayList<ScopeInfo>();

        scopeInfoList = scopeInfoService.listAll();

        if (scopeInfoList != null) {
            return ServerResponse.createBySuccess(scopeInfoList);
        } else {
            return ServerResponse.createByErrorMessage("获取数据失败！");
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/bannerimg")
    @ResponseBody
    public ServerResponse<List<BannerImage>> listAllBannerImg() {
        List<BannerImage> bannerImgList = new ArrayList<BannerImage>();

        bannerImgList = bannerImageService.listAll();

        if (bannerImgList != null) {
            return ServerResponse.createBySuccess(bannerImgList);
        } else {
            return ServerResponse.createByErrorMessage("获取数据失败！");
        }
    }
}
